package com.easy.common.util.file;
import java.io.*;
import java.util.*;
import javax.crypto.Cipher;
import sun.misc.BASE64Encoder;

import com.easy.common.constant.AuthKeys;
import com.easy.common.constant.Const;
import com.easy.common.constant.TokenConst;
import com.easy.common.exception.InvalidParamException;
import com.easy.common.util.SecurityUtility;
import com.easy.common.util.Utility;

public class FileUtility {
	private static Map extMap = new HashMap();

	public FileUtility()
	{
	}

	public static void setExt(Object key, Object value)
	{
		extMap.put(key, value);
	}

	public static void removeExt(Object key)
	{
		extMap.remove(key);
	}

	public static String appendFileName(String path, String name)
	{
		if (Utility.isNullorEmpty(path))
			return name;
		String lastChar = path.substring(path.length() - 1);
		if (lastChar.equals(TokenConst.FILE_SEPERATOR))
			return (new StringBuilder(String.valueOf(path))).append(name).toString();
		else
			return (new StringBuilder(String.valueOf(path))).append(TokenConst.FILE_SEPERATOR).append(name).toString();
	}

	public static void loadLine2Array(String fileName, int startLine, int endLine, List rtList)
		throws Exception
	{
		loadLine2Array(fileName, startLine, endLine, rtList, Const.DEFAULT_CODE);
	}

	public static void loadLine2Array(String fileName, int startLine, int endLine, List rtList, String charSet)
		throws Exception
	{
		InputStream ins = null;
		File file = new File(fileName);
		if (!file.exists())
		{
			try
			{
				if (ins != null)
					ins.close();
			}
			catch (Exception exception1) { }
			return;
		}
		try
		{
			ins = new FileInputStream(file);
			loadLine2Array(ins, startLine, endLine, rtList, charSet);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (ins != null)
					ins.close();
			}
			catch (Exception exception2) { }
		}
		return;
	}

	public static void loadLine2Array(String fileName, int startLine, List rtList)
		throws Exception
	{
		loadLine2Array(fileName, startLine, 0x7fffffff, rtList, Const.DEFAULT_CODE);
	}

	public static void loadLine2Array(String fileName, List rtList)
		throws Exception
	{
		loadLine2Array(fileName, 0, 0x7fffffff, rtList, Const.DEFAULT_CODE);
	}

	public static void loadLine2Array(String fileName, List rtList, String charSet)
		throws Exception
	{
		loadLine2Array(fileName, 0, 0x7fffffff, rtList, charSet);
	}

	public static void storeString2File(String fileName, String content)
		throws Exception
	{
		OutputStream outs = null;
		try
		{
			File file = new File(fileName);
			File outDir = file.getParentFile();
			if (!outDir.exists())
				outDir.mkdirs();
			if (!file.exists())
				file.createNewFile();
			if (!file.canWrite())
				file.setWritable(true);
			outs = new FileOutputStream(file);
			storeString2File(outs, content, Const.DEFAULT_CODE);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (outs != null)
					outs.close();
			}
			catch (Exception exception1) { 
				throw exception1;
			}
		}
		return;
	}

	public static void storeArray2Line(String fileName, List rtList)
		throws Exception
	{
		storeArray2Line(fileName, rtList, Const.DEFAULT_CODE);
	}

	public static void storeArray2Line(String fileName, List rtList, String charSet)
		throws Exception
	{
		OutputStream outs = null;
		try
		{
			File file = new File(fileName);
			File outDir = file.getParentFile();
			if (!outDir.exists())
				outDir.mkdirs();
			if (!file.exists())
				file.createNewFile();
			if (!file.canWrite())
				file.setWritable(true);
			outs = new FileOutputStream(file);
			storeArray2Line(outs, rtList, charSet);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try{
				if (outs != null)
					outs.close();
			}
			catch (Exception exception1) { 
				throw exception1;
			}
		}
		return;
	}

	public static void copyFile(String file, OutputStream out)
	{
		InputStream in = null;
		try
		{
			in = new FileInputStream(file);
			byte buff[] = new byte[1024];
			for (int readLength = 0; (readLength = in.read(buff)) > 0;)
				out.write(buff, 0, readLength);
			out.flush();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		try
		{
			if (in != null)
				in.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public static void xcopyFile(String srcFile, String destFile)
		throws Exception
	{
		copyFile(srcFile, destFile, null, true, false);
	}

	public static void copyFile(String srcFile, String destFile)
		throws Exception
	{
		copyFile(srcFile, destFile, null, false, false);
	}

	private static void copyFile(String srcFile, String destFile, List msrgList, boolean isDelete, boolean setModifyTime)
		throws Exception
	{
		copyFile0(srcFile, destFile, msrgList, isDelete, setModifyTime, null, null, 0, 0);
	}

	public static void encryptFile(String srcFile)
		throws Exception
	{
		String destFile = (new StringBuilder(String.valueOf(srcFile))).append("_encrypt").toString();
		copyFileEncrypt(srcFile, destFile);
		(new File(srcFile)).delete();
		(new File(destFile)).renameTo(new File(srcFile));
	}

	public static void decryptFile(String srcFile)
		throws Exception
	{
		String destFile = (new StringBuilder(String.valueOf(srcFile))).append("_decrypt").toString();
		copyFileDecrypt(srcFile, destFile);
		(new File(srcFile)).delete();
		(new File(destFile)).renameTo(new File(srcFile));
	}

	public static void copyFileEncrypt(String srcFile, String destFile)
		throws Exception
	{
		copyFile0(srcFile, destFile, null, false, false, (new BASE64Encoder()).encode(getFileName(srcFile).getBytes()).toCharArray(), AuthKeys.getSalt(null), AuthKeys.getItCnt(null), 1);
	}

	public static void copyFileDecrypt(String srcFile, String destFile)
		throws Exception
	{
		copyFile0(srcFile, destFile, null, false, false, (new BASE64Encoder()).encode(getFileName(srcFile).getBytes()).toCharArray(), AuthKeys.getSalt(null), AuthKeys.getItCnt(null), 2);
	}

	private static void copyFile0(String srcFile, String destFile, List msrgList, boolean isDelete, boolean setModifyTime, char passWord[], byte salt[], int itCnt, 
			int mode)
		throws Exception
	{
		InputStream ins;
		OutputStream outs;
		File inFile;
		ins = null;
		outs = null;
		inFile = null;
		File outFile;
		inFile = new File(srcFile);
		outFile = new File(destFile);
		if (!inFile.exists())
		{
			try
			{
				if (ins != null)
					ins.close();
			}
			catch (Exception exception1) { }
			try
			{
				if (outs != null)
					outs.close();
			}
			catch (Exception exception2) { }
			return;
		}
		try
		{
			File outDir = outFile.getParentFile();
			if (!outDir.exists())
				outDir.mkdirs();
			if (!outFile.exists())
				outFile.createNewFile();
			if (!outFile.canWrite())
				outFile.setWritable(true);
			ins = new FileInputStream(inFile);
			if (passWord != null)
			{
				Cipher cipher = (Cipher)extMap.get("cipher");
				if (cipher != null)
					ins = SecurityUtility.buildPassWordInputStream(cipher, ins);
				else
					ins = SecurityUtility.buildPassWordInputStream(passWord, salt, itCnt, mode, ins);
			}
			outs = new FileOutputStream(outFile);
			if (msrgList != null)
				msrgList.add((new StringBuilder("Copy ")).append(srcFile).append(" to ").append(destFile).toString());
			byte buff[] = new byte[1024];
			for (int readLength = 0; (readLength = ins.read(buff)) > 0;)
			{
				outs.write(buff, 0, readLength);
				outs.flush();
			}

			ins.close();
			outs.close();
			if (setModifyTime)
				outFile.setLastModified(inFile.lastModified());
			if (isDelete && inFile.exists())
			{
				if (msrgList != null)
					msrgList.add((new StringBuilder(String.valueOf(srcFile))).append(" is deleted after being copyed").toString());
				inFile.delete();
			}
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (ins != null)
					ins.close();
				if (outs != null)
					outs.close();
			}
			catch (Exception exception3) { }
		}
		return;
	}

	public static void storeFileFromStream(InputStream ins, String destFile)
		throws Exception
	{
		storeFileFromStream(ins, destFile, null, null, -1, -1);
	}

	public static void storeFileFromStreamDecrypt(String pass, InputStream ins, String destFile)
		throws Exception
	{
		storeFileFromStream(ins, destFile, (new BASE64Encoder()).encode(pass.getBytes()).toCharArray(), AuthKeys.getSalt(null), AuthKeys.getItCnt(null), 2);
	}

	public static void storBytes2File(String fileName, byte srcBuf[])
		throws Exception
	{
		storBytes2File(fileName, srcBuf, 0, srcBuf.length);
	}

	public static void storBytes2File(String fileName, byte srcBuf[], int offset, int length)
		throws Exception
	{
		OutputStream outs = null;
		try
		{
			File outFile = new File(fileName);
			File outDir = outFile.getParentFile();
			if (!outDir.exists())
				outDir.mkdirs();
			if (!outFile.exists())
				outFile.createNewFile();
			if (!outFile.canWrite())
				outFile.setWritable(true);
			outs = new FileOutputStream(outFile);
			outs.write(srcBuf, offset, length);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (outs != null)
					outs.close();
			}
			catch (Exception exception1) { }
		}
		return;
	}

	public static void storeFileFromStream(InputStream ins, String destFile, char passWord[], byte salt[], int itCnt, int mode)
		throws Exception
	{
		OutputStream outs = null;
		try
		{
			File outFile = new File(destFile);
			File outDir = outFile.getParentFile();
			if (!outDir.exists())
				outDir.mkdirs();
			if (!outFile.exists())
				outFile.createNewFile();
			if (!outFile.canWrite())
				outFile.setWritable(true);
			if (passWord != null)
				ins = SecurityUtility.buildPassWordInputStream(passWord, salt, itCnt, mode, ins);
			outs = new FileOutputStream(outFile);
			byte buff[] = new byte[1024];
			for (int readLength = 0; (readLength = ins.read(buff)) > 0;)
			{
				outs.write(buff, 0, readLength);
				outs.flush();
			}

		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (outs != null)
					outs.close();
			}
			catch (Exception exception1) { }
		}
		return;
	}

	public static void extractUpdateFile(String srcDirName, String destDirName, List msrgList, String cpDateStr)
		throws Exception
	{
		Date cpDate = Utility.parseDate(cpDateStr);
		copyDir(srcDirName, destDirName, msrgList, new TimeFileFilter(cpDate, true), false, true);
	}

	public static void copyDirCiph(String srcDirName, String destDirName, List msrgList, FileFilter filter, boolean copyEmptyDir, boolean setModifyTime, char passWord[], byte salt[], 
			int itCnt, int mode)
		throws Exception
	{
		copyDirImp0(srcDirName, destDirName, msrgList, filter, false, copyEmptyDir, setModifyTime, passWord, salt, itCnt, mode);
	}

	public static void copyDir(String srcDirName, String destDirName)
		throws Exception
	{
		copyDirImp(srcDirName, destDirName, null, null, false, true, true);
	}

	public static void copyDir(String srcDirName, String destDirName, List msrgList, FileFilter filter, boolean copyEmptyDir, boolean setModifyTime)
		throws Exception
	{
		copyDirImp(srcDirName, destDirName, msrgList, filter, false, copyEmptyDir, setModifyTime);
	}

	public static void xcopyDir(String srcDirName, String destDirName, List msrgList, FileFilter filter, boolean copyEmptyDir, boolean setModifyTime)
		throws Exception
	{
		copyDirImp(srcDirName, destDirName, msrgList, filter, true, copyEmptyDir, setModifyTime);
	}

	private static void copyDirImp(String srcDirName, String destDirName, List msrgList, FileFilter filter, boolean isDelete, boolean copyEmptyDir, boolean setModifyTime)
		throws Exception
	{
		copyDirImp0(srcDirName, destDirName, msrgList, filter, isDelete, copyEmptyDir, setModifyTime, null, null, 0, 0);
	}

	private static void copyDirImp0(String srcDirName, String destDirName, List msrgList, FileFilter filter, boolean isDelete, boolean copyEmptyDir, boolean setModifyTime, char passWord[], 
			byte salt[], int itCnt, int mode)
		throws Exception
	{
		File srcDir = new File(srcDirName);
		if (!srcDir.exists() || !srcDir.isDirectory())
			throw new InvalidParamException("源文件目录输入不正确！");
		String fileNameArray[] = srcDir.list();
		for (int i = 0; i < fileNameArray.length; i++)
		{
			String srcFileName = appendFileName(srcDirName, fileNameArray[i]);
			String destFileName = appendFileName(destDirName, fileNameArray[i]);
			File srcFile = new File(srcFileName);
			if (srcFile.isFile())
			{
				if (filter == null)
				{
					if (passWord == null)
						copyFile(srcFileName, destFileName, msrgList, isDelete, setModifyTime);
					else
						copyFile0(srcFileName, destFileName, msrgList, isDelete, setModifyTime, passWord, salt, itCnt, mode);
				} else
				if (filter.accept(srcFile))
					copyFile0(srcFileName, destFileName, msrgList, isDelete, setModifyTime, passWord, salt, itCnt, mode);
			} else
			if (srcFile.isDirectory())
			{
				String subDirFileArray[] = srcFile.list();
				if (subDirFileArray.length < 1)
				{
					if (copyEmptyDir)
						(new File(destFileName)).mkdirs();
				} else
				{
					copyDirImp0(srcFileName, destFileName, msrgList, filter, isDelete, copyEmptyDir, setModifyTime, passWord, salt, itCnt, mode);
					File destFile = new File(destFileName);
					String fileList[] = destFile.list();
					if (fileList == null || fileList.length < 1)
						destFile.delete();
				}
			}
		}

	}

	public static void loadLine2Array(InputStream in, int startLine, int endLine, List rtList, String charSet)
		throws Exception
	{
		LineNumberReader reader = new LineNumberReader(new InputStreamReader(in, charSet));
		String str = null;
		for (int i = 0; (str = reader.readLine()) != null; i++)
		{
			if (i < startLine)
				continue;
			if (i > endLine)
				break;
			rtList.add(str);
		}

	}

	public static byte[] loadFile2Bytes(String fileName)
		throws Exception
	{
		InputStream in = null;
		File file = new File(fileName);
		Exception exception;
		if (!file.exists())
		{
			try
			{
				if (in != null)
					in.close();
			}
			catch (Exception exception1) { }
			return null;
		}
		byte abyte0[];
		try
		{
			in = new FileInputStream(fileName);
			int fileLength = (int)file.length();
			byte buff[] = new byte[fileLength];
			in.read(buff, 0, fileLength);
			abyte0 = buff;
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally { }
		try
		{
			if (in != null)
				in.close();
		}
		catch (Exception exception2) { }
		return abyte0;
	}

	public static StringBuffer loadLine2Buff(InputStream in, int startLine, int endLine, String encode)
		throws Exception
	{
		StringBuffer rtBuffer = new StringBuffer();
		encode = encode != null ? encode : "UTF-8";
		LineNumberReader reader = new LineNumberReader(new InputStreamReader(in, encode));
		String str = null;
		for (int i = 0; (str = reader.readLine()) != null; i++)
		{
			if (i < startLine)
				continue;
			if (i > endLine)
				break;
			rtBuffer.append(str);
			rtBuffer.append("\n");
		}

		if (rtBuffer.length() > 1)
			rtBuffer.delete(rtBuffer.length() - 1, rtBuffer.length());
		return rtBuffer;
	}

	public static StringBuffer loadLine2Buff(String fileName, int startLine, int endLine, String encode)
		throws Exception
	{
		StringBuffer rtBuffer;
		InputStream ins;
		rtBuffer = null;
		ins = null;
		File file;
		StringBuffer stringbuffer;
		file = new File(fileName);
		if (!file.exists())return rtBuffer ;
		try
		{
			ins = new FileInputStream(file);
			rtBuffer = loadLine2Buff(ins, startLine, endLine, encode);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (ins != null)
					ins.close();
			}
			catch (Exception exception2) {throw exception2; }
		}
		return rtBuffer;
	}

	public static StringBuffer loadLine2Buff(String fileName, int startLine, String encode)
		throws Exception
	{
		return loadLine2Buff(fileName, startLine, 0x7fffffff, encode);
	}

	public static StringBuffer loadLine2Buff(String fileName, int startLine)
		throws Exception
	{
		return loadLine2Buff(fileName, startLine, 0x7fffffff, Const.DEFAULT_CODE);
	}

	public static StringBuffer loadLine2Buff(String fileName, String encode)
		throws Exception
	{
		return loadLine2Buff(fileName, 0, 0x7fffffff, encode);
	}

	public static StringBuffer loadLine2Buff(String fileName)
		throws Exception
	{
		return loadLine2Buff(fileName, 0, 0x7fffffff, Const.DEFAULT_CODE);
	}

	public static void load2Map(String fileName, Map rtMap)
		throws Exception
	{
		ArrayList propList = new ArrayList();
		loadLine2Array(fileName, propList);
		for (int i = 0; i < propList.size(); i++)
		{
			String line = propList.get(i).toString().trim();
			if (line.length() >= 1 && !line.startsWith("#"))
			{
				int tmpIndex = line.indexOf("=");
				if (tmpIndex >= 0)
				{
					String name = line.substring(0, tmpIndex).trim();
					String value = line.substring(tmpIndex + 1).trim();
					rtMap.put(name, value);
				}
			}
		}

	}

	public static void storeArray2Line(OutputStream out, List list, String charSet)
		throws Exception
	{
		OutputStreamWriter writer;
		if (list == null)
			return;
		writer = new OutputStreamWriter(out, charSet);
		try
		{
			for (int i = 0; i < list.size(); i++)
			{
				writer.write(list.get(i).toString());
				writer.write("\r\n");
				writer.flush();
			}

		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (writer != null)
					writer.close();
			}
			catch (Exception exception1) { 
				throw exception1;
			}
		}
		return;
	}

	public static void storeString2File(OutputStream out, String str, String charSet)
		throws Exception
	{
		OutputStreamWriter writer;
		if (str == null)
			return;
		writer = null;
		try
		{
			if (charSet != null)
				writer = new OutputStreamWriter(out, charSet);
			else
				writer = new OutputStreamWriter(out);
			writer.write(str);
			writer.flush();
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (writer != null)
					writer.close();
			}
			catch (Exception exception1) {
				throw exception1;
			}
		}
		return;
	}

	public static String getFileNameNoExt(String filePath)
	{
		if (filePath == null)
			return null;
		String rtName = getFileName(filePath);
		int pointIndex = rtName.lastIndexOf(".");
		if (pointIndex < 0)
			return rtName;
		if (pointIndex == 0)
			return "";
		else
			return rtName.substring(0, pointIndex);
	}

	public static String getFileExtName(String filePath)
	{
		if (filePath == null)
			return "";
		String rtName = getFileName(filePath);
		int pointIndex = rtName.lastIndexOf(".");
		if (pointIndex < 0)
			return "";
		if (pointIndex == rtName.length() - 1)
			return "";
		else
			return rtName.substring(pointIndex + 1, rtName.length());
	}

	public static String getFileName(String filePath)
	{
		if (filePath == null)
			return null;
		int startIndex = 0;
		if (filePath.lastIndexOf("\\") >= 0)
			startIndex = filePath.lastIndexOf("\\") + 1;
		else
		if (filePath.lastIndexOf("/") >= 0)
			startIndex = filePath.lastIndexOf("/") + 1;
		return filePath.substring(startIndex, filePath.length());
	}

	public static String getFilePath(String fileName)
	{
		if (fileName == null)
			return "";
		int tmpIndex = -1;
		if (fileName.lastIndexOf("\\") >= 0)
			tmpIndex = fileName.lastIndexOf("\\");
		else
		if (fileName.lastIndexOf("/") >= 0)
			tmpIndex = fileName.lastIndexOf("/");
		if (tmpIndex < 0)
			return "";
		else
			return fileName.substring(0, tmpIndex);
	}

	public static void replaceInFile(String fileName, String srcStr, String destStr)
		throws Exception
	{
		List rules = new ArrayList();
		rules.add(new String[] {
			srcStr, destStr
		});
		replaceInFile(fileName, rules);
	}

	public static void replaceInFile(String fileName, List rules)
		throws Exception
	{
		try
		{
			ArrayList lineList = new ArrayList();
			loadLine2Array(fileName, lineList);
			for (int i = 0; i < lineList.size(); i++)
			{
				String lineStr = (String)lineList.get(i);
				for (int j = 0; j < rules.size(); j++)
				{
					String ruleArray[] = (String[])rules.get(j);
					if (lineStr.indexOf(ruleArray[0]) >= 0)
					{
						lineStr = lineStr.replace(ruleArray[0], ruleArray[1]);
						lineList.set(i, lineStr);
					}
				}

			}

			storeArray2Line(fileName, lineList);
		}
		catch (Exception ex)
		{
			throw ex;
		}
	}

	public static byte[] loadByteArrayFromFile(String fileName, char passWord[], byte salt[], int itCnt, int mode)
		throws Exception
	{
		ByteArrayOutputStream out;
		InputStream in;
		Exception exception;
		out = null;
		in = null;
		byte abyte0[];
		try
		{
			in = new BufferedInputStream(new FileInputStream(fileName));
			if (salt != null)
				in = SecurityUtility.buildPassWordInputStream(passWord, salt, itCnt, mode, in);
			byte buff[] = new byte[10240];
			int readLen = 0;
			out = new ByteArrayOutputStream();
			while ((readLen = in.read(buff)) > 0) 
			{
				out.write(buff, 0, readLen);
				out.flush();
			}
			abyte0 = out.toByteArray();
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally {
			try
			{
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			}
			catch (Exception exception1) { 
				throw exception1;
			}
		}
		return abyte0;
	}

	public static void deleteAll(String file)
	{
		deleteAll(new File(file));
	}

	public static void deleteAll(File file)
	{
		if (file.isFile())
		{
			file.delete();
			return;
		}
		File fileList[] = file.listFiles();
		for (int i = 0; fileList != null && i < fileList.length; i++)
			deleteAll(fileList[i]);

		file.delete();
	}

	public static void deleteFile(List fileList)
		throws Exception
	{
		deleteFile(fileList, null);
	}

	public static void deleteFile(List fileList, String rootDir)
		throws Exception
	{
		if (fileList == null || fileList.size() < 1)
			return;
		for (int i = 0; i < fileList.size(); i++)
		{
			String currPath = (String)fileList.get(i);
			if (rootDir != null)
				currPath = (new StringBuilder(String.valueOf(rootDir))).append(File.separator).append(fileList.get(i)).toString();
			deleteAll(currPath);
		}

	}

	public static String windows2Unix(String fileName)
	{
		if (fileName == null)
			return "";
		else
			return fileName.replace('\\', '/');
	}

	public static String unix2Windows(String fileName)
	{
		if (fileName == null)
			return "";
		else
			return fileName.replace('/', '\\');
	}

	public static void outPutFileName(String fileName, String outputFile)
		throws Exception
	{
		ArrayList fileList = new ArrayList();
		outPutFileName(new File(fileName), fileList);
		if (outputFile != null)
		{
			storeArray2Line(outputFile, fileList);
		} else
		{
			for (int i = 0; i < fileList.size(); i++);
		}
	}

	private static void outPutFileName(File file, ArrayList fileList)
		throws Exception
	{
		if (!file.exists())
			return;
		if (file.isFile())
			fileList.add(file.getAbsolutePath());
		else
		if (file.isDirectory())
		{
			File files[] = file.listFiles();
			for (int i = 0; i < files.length; i++)
				outPutFileName(files[i], fileList);

		}
	}

	public static boolean isFileEqual(String file1, String file2)
		throws Exception
	{
		return isFileEqual(new File(file1), new File(file2));
	}

	public static boolean isFileEqual(File f1, File f2)
		throws Exception
	{
		boolean rtBool;
		InputStream in1;
		InputStream in2;
		rtBool = false;
		in1 = null;
		in2 = null;
		in1 = new FileInputStream(f1);
		in2 = new FileInputStream(f2);
		if (f1.length() != f2.length())
		{
			if (in1 != null)
				try
				{
					in1.close();
				}
				catch (Exception exception1) { }
			if (in2 != null)
				try
				{
					in2.close();
				}
				catch (Exception exception2) { }
			return false;
		}
		if (f1.lastModified() == f2.lastModified())
		{
			if (in1 != null)
				try
				{
					in1.close();
				}
				catch (Exception exception3) { }
			if (in2 != null)
				try
				{
					in2.close();
				}
				catch (Exception exception4) { }
			return true;
		}
		byte buf1[];
		byte buf2[];
		buf1 = new byte[1024];
		buf2 = new byte[1024];
		for (int i = 0; i < 1024; i++)
		{
			buf1[i] = 0;
			buf2[i] = 0;
		}

		int len1;
		int len2;
		len1 = in1.read(buf1);
		len2 = in2.read(buf2);
		if (len1 != len2)
		{
			if (in1 != null)
				try
				{
					in1.close();
				}
				catch (Exception exception5) { }
			if (in2 != null)
				try
				{
					in2.close();
				}
				catch (Exception exception6) { }
			return false;
		}
		if (len1 < 1) return false;
		try
		{
			rtBool = Arrays.equals(buf1, buf2);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally{
			try
			{
				if (in1 != null)in1.close();
				if (in2 != null)in2.close();
			}
			catch (Exception exception9) { }
		}
		return rtBool;
	}

	public static void copyNotExists(String srcDir, String destDir, String outDir, List msrgList)
		throws Exception
	{
		if (Utility.isNullorEmpty(srcDir) || Utility.isNullorEmpty(destDir))
			return;
		File srcDirFile = new File(srcDir);
		if (!srcDirFile.isDirectory() || !srcDirFile.exists())
			return;
		String fileNameArray[] = srcDirFile.list();
		for (int i = 0; i < fileNameArray.length; i++)
		{
			String srcFilePath = (new StringBuilder(String.valueOf(srcDir))).append(File.separator).append(fileNameArray[i]).toString();
			String destFilePath = (new StringBuilder(String.valueOf(destDir))).append(File.separator).append(fileNameArray[i]).toString();
			String outFilePath = (new StringBuilder(String.valueOf(outDir))).append(File.separator).append(fileNameArray[i]).toString();
			File currFile = new File(srcFilePath);
			File currDestFile = new File(destFilePath);
			if (currFile.isFile())
			{
				if (!currDestFile.exists())
				{
					copyFile(srcFilePath, outFilePath);
					if (msrgList != null)
						msrgList.add((new StringBuilder("Add file>>")).append(fileNameArray[i]).append(" to ").append(outDir).toString());
				}
			} else
			{
				copyNotExists(srcFilePath, destFilePath, outFilePath, msrgList);
			}
		}

	}

	public static void copyDirDiff(String newDir, String oldDir, String outDir, List msrgList, boolean copyEmpyDir)
		throws Exception
	{
		FileDifFilter filter = new FileDifFilter(newDir, oldDir);
		copyDir(newDir, outDir, msrgList, filter, copyEmpyDir, true);
	}

	public static void setLastModified(String fileName, long time)
	{
		if (Utility.isNullorEmpty(fileName))
		{
			return;
		} else
		{
			File file = new File(fileName);
			setLastModified(file, time);
			return;
		}
	}

	public static void setLastModified(File file, long time)
	{
		if (!file.exists())
			return;
		if (file.isFile())
		{
			file.setLastModified(time);
		} else
		{
			File fileList[] = file.listFiles();
			for (int i = 0; i < fileList.length; i++)
				setLastModified(fileList[i], time);

		}
	}

	public static void setWritable(String fileName)
	{
		if (Utility.isNullorEmpty(fileName))
		{
			return;
		} else
		{
			File file = new File(fileName);
			setWritable(file, true);
			return;
		}
	}

	public static void setWritable(String fileName, boolean writable)
	{
		if (Utility.isNullorEmpty(fileName))
		{
			return;
		} else
		{
			File file = new File(fileName);
			setWritable(file, writable);
			return;
		}
	}

	public static void setWritable(File file, boolean writable)
	{
		if (!file.exists())
			return;
		if (file.isFile())
		{
			file.setWritable(writable);
		} else
		{
			File fileList[] = file.listFiles();
			for (int i = 0; i < fileList.length; i++)
				setWritable(fileList[i], writable);

		}
	}

	public static String getMaxNameFile(String dirDName)
	{
		return getMaxNameFile(new File(dirDName), null);
	}

	public static String getMinNameFile(String dirDName)
	{
		return getMinNameFile(new File(dirDName), null);
	}

	public static String getMaxNameFile(File dir)
	{
		String fileArray[] = sortFileList(dir, ((String) (null)));
		if (fileArray == null || fileArray.length < 1)
			return null;
		else
			return fileArray[0];
	}

	public static String getMinNameFile(File dir)
	{
		String fileArray[] = sortFileList(dir, false, null, null);
		if (fileArray == null || fileArray.length < 1)
			return null;
		else
			return fileArray[0];
	}

	public static String[] sortFileList(String dirDName)
	{
		return sortFileList(dirDName, true, null);
	}

	public static String[] sortFileList(File file)
	{
		return sortFileList(file, true, null, null);
	}

	public static String[] sortFileList(String dirDName, boolean isAsc)
	{
		File file = new File(dirDName);
		return sortFileList(file, isAsc, null, null);
	}

	public static String getMaxNameFile(String dirDName, String prefix)
	{
		return getMaxNameFile(new File(dirDName), prefix);
	}

	public static String getMinNameFile(String dirDName, String prefix)
	{
		return getMinNameFile(new File(dirDName), prefix);
	}

	public static String getMaxNameFile(File dir, String prefix)
	{
		String fileArray[] = sortFileList(dir, prefix);
		if (fileArray == null || fileArray.length < 1)
			return null;
		else
			return fileArray[0];
	}

	public static String getMinNameFile(File dir, String prefix)
	{
		String fileArray[] = sortFileList(dir, false, prefix, null);
		if (fileArray == null || fileArray.length < 1)
			return null;
		else
			return fileArray[0];
	}

	public static String[] sortFileList(String dirDName, String prefix)
	{
		return sortFileList(dirDName, true, prefix);
	}

	public static String[] sortFileList(File file, String prefix)
	{
		return sortFileList(file, true, prefix, null);
	}

	public static String[] sortFileList(String dirDName, boolean isAsc, String prefix)
	{
		File file = new File(dirDName);
		return sortFileList(file, isAsc, prefix, null);
	}

	public static String[] sortFileList(File file, boolean isAsc)
	{
		return sortFileList(file, isAsc, null, null);
	}

	public static String[] sortFileList(File file, boolean isAsc, String prefix, String postfix)
	{
		if (file == null || !file.exists() || !file.isDirectory())
			return null;
		TreeSet set = new TreeSet();
		String fileArray[] = file.list();
		for (int i = 0; i < fileArray.length; i++)
			set.add(fileArray[i]);

		List list = new ArrayList();
		for (Iterator iObj = set.iterator(); iObj.hasNext();)
		{
			String name = iObj.next().toString();
			if ((prefix == null || name.startsWith(prefix)) && (postfix == null || name.endsWith(postfix)))
				list.add(name);
		}

		int elemCnt = list.size();
		String rtArray[] = new String[elemCnt];
		if (isAsc)
		{
			for (int i = 0; i < elemCnt; i++)
				rtArray[i] = (String)list.get(i);

		} else
		{
			int index = 0;
			for (int i = elemCnt - 1; i >= 0; i--)
				rtArray[index++] = (String)list.get(i);

		}
		return rtArray;
	}
}
